# Contributor: rubicon <rubicon@mailo.com>
# Maintainer: rubicon <rubicon@mailo.com>
pkgname=emacspeak
pkgver=56.0
pkgrel=1
pkgdesc="Emacs audio desktop for eyes-free computing"
url="http://emacspeak.sourceforge.net/"
arch="all"
license="GPL-2.0-or-later"
depends="cmd:emacs $pkgname-sounds"
depends_doc="texinfo-doc emacs-doc"
makedepends="emacs-nox"
options="!check"
subpackages="
	$pkgname-sounds:ems_sounds:noarch
	$pkgname-doc
	$pkgname-misc:ems_misc:noarch
	"
source="https://github.com/tvraman/emacspeak/releases/download/$pkgver/emacspeak-$pkgver.tar.bz2
	rebuild-pickup-c.patch
	fix-tclespeak-path.patch
	remote-espeak-server.patch
	directories.patch
	emacspeak
	"

# espeak-ng is not available on s390x, so use the eflite speech server instead
if [ "$CARCH" = "s390x" ]; then
	depends="$depends $pkgname-server-eflite"
else
	depends="$depends $pkgname-server-espeak"
	makedepends="$makedepends espeak-ng-dev tcl-dev"
	subpackages="$subpackages $pkgname-server-espeak:ems_espeak"
fi

_emsdir=usr/share/emacs/site-lisp/"$pkgname"

build() {
	make config
	make
	make -C etc/pickup-c
	if [ "$CARCH" != "s390x" ]; then
		make espeak
	fi
}

package() {
	local emspkgdir="$pkgdir"/"$_emsdir"
	local docdir="$pkgdir"/usr/share/doc/"$pkgname"
	local infodir="$pkgdir"/usr/share/info/"$pkgname"

	mkdir -vp "$(dirname $emspkgdir)"
	cp -a "$builddir" "$emspkgdir"
	cd "$emspkgdir"

	# clean unneeded files
	find . -type f \( \
		-name Makefile -o \
		-name .nosearch -o \
		-name '.*spell*' \) \
		-delete
	rm -vr servers/linux-outloud
	rm -v	.*ignore README run \
		runq etc/pickup-c/pickup.o

	# fix ownerships and permissions
	chown -Rf root:root .
	chmod 755 .

	# move documentation
	mkdir -vp "$docdir"
	mv -v README.md etc/COPYRIGHT etc/NEWS* \
		etc/applications.* etc/tips.* \
		blog-archive attic-readme \
		lisp/elpa-readme \
		"$docdir"
	mkdir -vp "$(dirname $infodir)"
	rm -v info/tips.html
	mv -v info "$infodir"

	install -vDm 755 \
		"$srcdir"/emacspeak \
		-t "$pkgdir"/usr/bin
}

ems_espeak() {
	pkgdesc="$pkgdesc (espeak-ng speech server)"
	depends="tclx"
	install_if="emacspeak espeak-ng"
	local servdir="$_emsdir"/servers

	install -vDm 755 \
		"$pkgdir"/"$servdir"/native-espeak/tclespeak.so \
		-t "$subpkgdir"/usr/lib/"$pkgname"

	# move supported speech servers
	amove	"$servdir"/tts-lib.tcl \
		"$servdir"/espeak \
		"$servdir"/ssh-espeak \
		"$servdir"/speech-server \
		"$servdir"/cloud-espeak \
		"$servdir"/cloud-notify
	# remove all others
	rm -vrf "$servdir"
}

ems_sounds() {
	pkgdesc="$pkgdesc (auditory icons / sound cues)"
	depends=""
	amove	"$_emsdir"/sounds
}

ems_misc() {
	pkgdesc="$pkgdesc (miscellaneous files & helper scripts)"
	depends="$pkgname"
	amove	"$_emsdir"/bash-utils \
		"$_emsdir"/js \
		"$_emsdir"/nyxttalk \
		"$_emsdir"/xsl \
		"$_emsdir"/stumpwm \
		"$_emsdir"/media
}

sha512sums="
9b18fada991b914ddd3ed30950e685c1c164434536c3dd119b9ed52db80e7e31b7399c72a584938bfb63daa7e1aa76a1f377d664cc1118ffa7f0a4cbdddf0818  emacspeak-56.0.tar.bz2
c619611ee7ef4c59e6f797a010f126d870a7dff1dc6d4109c16519637b6d6ee733879e9fb6080103abe3bb33a8156846ee09a00749d26bfaa5400dd9dc4cc527  rebuild-pickup-c.patch
abc9182d7d53b0df859c6d345ebc8a192e16aefbc318a85bcef903ee62586afdd0f11a75a90da62356c7fdbadcbc10ee268f5575d64f037971cf082db1d6b314  fix-tclespeak-path.patch
e1557a40e15c364c03d760ea9711c7015b3f127f5e3c17fc7ca6eca4a08c3ef4e0f14cad1f3cc67b05af581f1477de7a773fc9741fab1218346082eefe0594ce  remote-espeak-server.patch
e9fe798589a685bdebae168b2d50d738562e152d50795b6d7533152e553a410ca64854ca8298d25fba29dc803793456a5d15457aa2299c246ed5e8529ed924c0  directories.patch
a04a5f015a72c0ef453d3dd6b459fea4a339ad3a64276c66e7bd945b059e68f965a0fdf1d3f078b94bdf1345c5ca6a8bcb46419bb1b8ee56894ac63e0c25efb3  emacspeak
"
